﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;

namespace Excel.Expressions.Compiler
{
    internal class Range : IEnumerable<CellExpression>
    {
        private int _x1;
        private int _x2;
        private int _y1;
        private int _y2;

        public Range(int x1, int y1, int x2, int y2)
        {
            _x1 = x1;
            _y1 = y1;
            _x2 = x2;
            _y2 = y2;
            if (_x1 > _x2)
            {
                int temp = _x1;
                _x1 = _x2;
                _x2 = temp;
            }
            if (_y1 > _y2)
            {
                int temp = _y1;
                _y1 = _y2;
                _y2 = temp;
            }
        }

        #region IEnumerable<CellReference> Members

        public IEnumerable<CellExpression> Items
        {
            get
            {
                for (int i = _x1; i <= _x2; i++)
                {
                    for (int j = _y1; j <= _y2; j++)
                    {
                        yield return new CellReference(i, j);
                    }
                }
            }
        }

        public IEnumerator<CellExpression> GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        #endregion
    }
}
